package org.jivesoftware.spark.ui.transcript;
/**
 * @author SidneyRen E-mail:SidneyRen@hotmail.com
 * @version create time：2010-10-21 下午04:41:14
 * 类说明
 */
import org.jivesoftware.resource.Res;
import org.jivesoftware.spark.component.LoginDialogButton;
import org.jivesoftware.spark.component.RolloverButton;
import org.jivesoftware.spark.util.GraphicUtils;
import org.jivesoftware.spark.util.ResourceUtils;
import org.jivesoftware.spark.util.log.Log;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
	
	/**
	 * Conference Invitation UI.
	 *
	 * @author Derek DeMoro
	 */
	public class MessageTranscriptItem extends JPanel implements ActionListener {

		private String title;
		private String content;
		private String sendTime;
		
	    private RolloverButton acceptButton;

	    private String room;
	    private String inviter;
	    private String password;
	    final private JButton openAppButton;
	    final private JTextPane titleLabel;
	    private Action customAction;
	    private String invitationDateFormat = ((SimpleDateFormat)SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM)).toPattern();

	    public MessageTranscriptItem(String title, String content, String sendTime) {
	        setLayout(new GridBagLayout());
	        setBackground(new Color(255,255,255));
//	        setBorder(BorderFactory.createMatteBorder(1,1,1,1,new Color(230,239,249)));
	        titleLabel = new JTextPane();
	        titleLabel.setOpaque(false);
	        titleLabel.setEditable(false);
	        openAppButton =new LoginDialogButton();
	        openAppButton.setOpaque(false);
	        openAppButton.setVisible(false);
	        ResourceUtils.resButton(openAppButton, Res
					.getString("button.transcript.app.view"));
	        add(titleLabel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,1,5, 0), 0, 0));
	        add(openAppButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(0,3,1,0), 0, 0));
	        
	        final SimpleAttributeSet styles = new SimpleAttributeSet();
	        StyleConstants.setForeground(styles, new Color(13, 104, 196));
	        
	        Document document = titleLabel.getDocument();
	        try {
	        	document.insertString(0, title,styles);
	            document.insertString(document.getLength(), "  " + sendTime , styles);
	            
	            StyleConstants.setForeground(styles, new Color(0,0,0));
	            document.insertString(document.getLength(), "\n    "+content, styles);
	            
	        }
	        catch (BadLocationException e) {
	            Log.error(e);
	        }

	        titleLabel.addMouseListener(new MouseAdapter() {
	        	@Override
	        	public void mouseEntered(MouseEvent e) {
	        		// TODO Auto-generated method stub
	        		titleLabel.setBackground(new Color(230,239,249));
	        		setBackground(new Color(230,239,249));
	        		
	        		openAppButton.setVisible(true);
	        	}
	        	@Override
	        	public void mouseExited(MouseEvent e) {
	        		// TODO Auto-generated method stub
	        		titleLabel.setBackground(new Color(255,255,255));
	        		setBackground(new Color(255,255,255));
	        		
	        		openAppButton.setVisible(false);
	        	}
	        });
	        addMouseListener(new MouseAdapter() {
	        	@Override
	        	public void mouseEntered(MouseEvent e) {
	        		// TODO Auto-generated method stub
	        		titleLabel.setBackground(new Color(230,239,249));
	        		setBackground(new Color(230,239,249));
	        		
	        		openAppButton.setVisible(true);
	        	}
	        	@Override
	        	public void mouseExited(MouseEvent e) {
	        		// TODO Auto-generated method stub
	        		titleLabel.setBackground(new Color(255,255,255));
	        		setBackground(new Color(255,255,255));
	        		openAppButton.setVisible(false);
	        	}
			});
	        openAppButton.addMouseListener(new MouseAdapter() {
	        	public void mouseEntered(MouseEvent e) {
//	        		openAppButton.setForeground(new Color(0,0,255));
	        		openAppButton.setVisible(true);
	        		titleLabel.setBackground(new Color(230,239,249));
	        		setBackground(new Color(230,239,249));
	        		openAppButton.setCursor(GraphicUtils.HAND_CURSOR);
	        	}
	        	@Override
	        	public void mouseExited(MouseEvent e) {
	        		// TODO Auto-generated method stub
	        		openAppButton.setVisible(false);
	        		titleLabel.setBackground(new Color(255,255,255));
	        		setBackground(new Color(255,255,255));
//	        		openAppButton.setForeground(new Color(0,0,0));
	        		openAppButton.setCursor(GraphicUtils.DEFAULT_CURSOR);
	        	}
	        	@Override
	        	public void mouseClicked(MouseEvent e) {
	        		// TODO Auto-generated method stub
	        		if(null!=customAction){
	        			 customAction.actionPerformed(null);
	        		}
	        	}
	        });
	    }

	    public void actionPerformed(ActionEvent event) {
	        if (event.getSource() == acceptButton) {
//	            acceptInvitation();
	        }
	        else {
//	            rejectInvitation();
	        }
	    }
	    
	    public void setCustomAction(Action action){
	    	this.customAction = action;
	    }

	   

	    /**
	     * Removes this interface from it's parent.
	     */
	    private void removeUI() {
	        final Container par = getParent();
	        if (par != null) {
	            par.remove(this);
	            par.invalidate();
	            par.validate();
	            par.repaint();
	        }

	    }
	}
